<template>
  <div>
    <FixedOperation>
      <!-- 内容 -->
      <el-row :gutter="20">
        <!--物料分类-->
        <el-col :span="24" :xs="24">
          <div class="text-right">
            <el-button type="primary" @click="handleSubmit">保存</el-button>
            <!-- <el-button icon="Refresh" @click="handleReset(form)"
              >重置</el-button
            > -->
            <el-button @click="handleReturn">返回</el-button>
          </div>
        </el-col>
      </el-row>
    </FixedOperation>
    <div class="app-container product-form">
      <el-form
        ref="form"
        :model="formData"
        label-width="140px"
        :rules="formRules"
      >
        <el-collapse v-model="activeNames">
          <!-- 基本信息 -->
          <el-collapse-item name="基本信息" disabled>
            <template #title>
              <TitleName
                title="基本信息"
                icon="i-carbon-bookmark-filled"
                :titleStyle="{ color: '#000' }"
              ></TitleName>
            </template>
            <el-row>
              <el-col :span="12">
                <el-form-item label="名称" prop="name">
                  <el-input
                    v-model="formData.name"
                    clearable
                    placeholder="请输入名称"
                  >
                    <template #append
                      ><el-button icon="Search" @click="addProduct()"
                    /></template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="编号" prop="number">
                  <el-input
                    v-model="formData.number"
                    placeholder="请输入编号"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="分类" prop="categoryId">
                  <el-tree-select
                    class="w-full"
                    v-model="formData.categoryId"
                    placeholder="请选择分类"
                    :data="sortOptions"
                    :checkStrictly="true"
                    :props="{
                      value: 'id',
                      label: 'name',
                      children: 'children'
                    }"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="计量单位" prop="unit">
                  <el-select
                    class="w-full"
                    v-model="formData.unit"
                    filterable
                    suffix-icon="Search"
                    :suffix-transition="false"
                    placeholder="请选择计量单位"
                  >
                    <el-option
                      v-for="item in measurement_unit"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="板块类型" prop="plateType">
                  <el-select
                    class="w-full"
                    v-model="formData.plateType"
                    placeholder="请选择板块类型"
                  >
                    <el-option
                      v-for="item in ord_product_plate_type"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="产业板块" prop="industrialPlate">
                  <el-select
                    class="w-full"
                    v-model="formData.industrialPlate"
                    placeholder="请选择产业板块"
                  >
                    <el-option
                      v-for="item in ord_product_plate"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="简称" prop="abbreviation">
                  <el-input
                    v-model="formData.abbreviation"
                    placeholder="请输入简称"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="劳务服务" prop="laborService">
                  <el-radio-group v-model="formData.laborService">
                    <el-radio
                      v-for="item in yes_or_no"
                      :key="item.value"
                      :label="item.value"
                      >{{ item.label }}</el-radio
                    >
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="设备类型" prop="deviceType">
                  <el-radio-group v-model="formData.deviceType">
                    <el-radio
                      v-for="item in device_type"
                      :key="item.value"
                      :label="item.value"
                      >{{ item.label }}</el-radio
                    >
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="物料业务板块" prop="productBussinessPlate">
                  <el-select
                    class="w-full"
                    v-model="formData.productBussinessPlate"
                    placeholder="请选择物料业务板块"
                  >
                    <el-option
                      v-for="item in product_bussiness_plate"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="所属业务分类" prop="businessType">
                  <el-tree-select
                    class="w-full"
                    v-model="formData.businessType"
                    placeholder="请选择业务分类"
                    :data="businessTypeOption"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="服务期是否必填" prop="serviceDateRequired">
                  <el-radio-group v-model="formData.serviceDateRequired">
                    <el-radio
                      v-for="item in yes_or_no"
                      :key="item.value"
                      :label="item.value"
                      >{{ item.label }}</el-radio
                    >
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="产品同步位置" prop="syncLocationArr">
                  <el-select
                    class="w-full"
                    multiple
                    v-model="formData.syncLocationArr"
                    placeholder="请选择产品同步位置"
                  >
                    <el-option
                      v-for="item in ord_sync_location"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-collapse-item>
          <!-- 业务信息 -->
          <el-collapse-item name="业务信息">
            <template #title>
              <TitleName
                title="业务信息"
                icon="i-carbon-bookmark-filled"
                :titleStyle="{ color: '#000' }"
              ></TitleName>
            </template>
            <el-row>
              <el-col :span="12">
                <el-form-item label="销售价" prop="price">
                  <el-input v-model="formData.price" placeholder="请输入销售价"
                    ><template v-slot:append>元</template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="默认仓库" prop="defaultWarehouse">
                  <el-select
                    v-model="formData.defaultWarehouse"
                    placeholder="请输入仓库名称检索仓库"
                    class="w-full"
                    suffix-icon="Search"
                    remote-show-suffix
                    :suffix-transition="false"
                    filterable
                    remote
                    :remote-method="val => queryWhsLimitEnt(val)"
                    :loading="loadingSelect"
                    no-data-text="未查询到仓库"
                  >
                    <el-option
                      v-for="item in whsOptLimitEnt"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                  <!-- <el-select
                    class="w-full"
                    v-model="formData.defaultWarehouse"
                    placeholder="请选择默认仓库"
                  >
                    <el-option
                      v-for="item in warehouse_info"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select> -->
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="采购参考价格"
                  prop="purchaseReferencePrice"
                >
                  <el-input
                    v-model="formData.purchaseReferencePrice"
                    placeholder="请输入采购参考价格"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="默认货位" prop="defaultCargo">
                  <el-input
                    v-model="formData.defaultCargo"
                    placeholder="请输入默认货位"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="内部交易参考价格"
                  prop="tradingReferencePrice"
                >
                  <el-input
                    v-model="formData.tradingReferencePrice"
                    placeholder="请输入内部交易参考价格"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="最低销售价格" prop="minSellingPrice">
                  <el-input
                    v-model="formData.minSellingPrice"
                    placeholder="请输入最低销售价格"
                    ><template v-slot:append>元</template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="超订单入库比例"
                  prop="excessOrderInventoryRatio"
                >
                  <el-input
                    v-model="formData.excessOrderInventoryRatio"
                    placeholder="请输入超订单入库比例"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="最高销售价格" prop="maxSellingPrice">
                  <el-input
                    v-model="formData.maxSellingPrice"
                    placeholder="请输入最高销售价格"
                    ><template v-slot:append>元</template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="成本加成方式" prop="additionType">
                  <el-select
                    class="w-full"
                    v-model="formData.additionType"
                    placeholder="请选择成本加成方式"
                  >
                    <el-option
                      v-for="item in addition_method"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="成本加成比率" prop="additionRatio">
                  <el-input
                    v-model="formData.additionRatio"
                    placeholder="请输入成本加成比率"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="往来业务类型" prop="transactionType">
                  <el-select
                    class="w-full"
                    v-model="formData.transactionType"
                    placeholder="请选择往来业务类型"
                  >
                    <el-option
                      v-for="item in transaction_type"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="成本加价" prop="markup">
                  <el-input
                    v-model="formData.markup"
                    placeholder="请输入成本加价"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="安全库存" prop="safeInventory">
                  <el-input
                    v-model="formData.safeInventory"
                    placeholder="请输入安全库存"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="最低库存" prop="minInventory">
                  <el-input
                    v-model="formData.minInventory"
                    placeholder="请输入最低库存"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="最高库存" prop="maxInventory">
                  <el-input
                    v-model="formData.maxInventory"
                    placeholder="请输入最高库存"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="组装件" prop="assembly">
                  <el-radio-group v-model="formData.assembly">
                    <el-radio
                      v-for="item in yes_or_no"
                      :key="item.value"
                      :label="item.value"
                      >{{ item.label }}</el-radio
                    >
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="是否允许零出库" prop="zeroExitControl">
                  <el-radio-group v-model="formData.zeroExitControl">
                    <el-radio
                      v-for="item in yes_or_no"
                      :key="item.value"
                      :label="item.value"
                      >{{ item.label }}</el-radio
                    >
                  </el-radio-group>
                  <!-- <el-select
                    class="w-full"
                    v-model="formData.zeroExitControl"
                    placeholder="请选择是否允许零出库"
                  >
                    <el-option
                      v-for="item in yes_or_no"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select> -->
                </el-form-item>
              </el-col>
            </el-row>
          </el-collapse-item>
          <!-- 财务信息 -->
          <el-collapse-item name="财务信息">
            <template #title>
              <TitleName
                title="财务信息"
                icon="i-carbon-bookmark-filled"
                :titleStyle="{ color: '#000' }"
              ></TitleName>
            </template>
            <el-row>
              <el-col :span="12">
                <el-form-item label="参考成本" prop="referencePrice">
                  <el-input
                    v-model="formData.referencePrice"
                    placeholder="请输入参考成本"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="收入分摊" prop="incomeShare">
                  <el-radio-group v-model="formData.incomeShare">
                    <el-radio
                      v-for="item in yes_or_no"
                      :key="item.value"
                      :label="item.value"
                      >{{ item.label }}</el-radio
                    >
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="开票名称" prop="invoiceName">
                  <el-input
                    v-model="formData.invoiceName"
                    placeholder="请输入开票名称"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="批次核算" prop="batchAccounting">
                  <el-radio-group v-model="formData.batchAccounting">
                    <el-radio
                      v-for="item in yes_or_no"
                      :key="item.value"
                      :label="item.value"
                      >{{ item.label }}</el-radio
                    >
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="开票规格型号" prop="invoiceType">
                  <el-input
                    v-model="formData.invoiceType"
                    placeholder="请输入开票规格型号"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="项目核算" prop="projectAccounting">
                  <el-radio-group v-model="formData.projectAccounting">
                    <el-radio
                      v-for="item in yes_or_no"
                      :key="item.value"
                      :label="item.value"
                      >{{ item.label }}</el-radio
                    >
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <!-- <el-col :span="12">
                <el-form-item label="计量单位" prop="unit">
                  <el-select
                    class="w-full"
                    v-model="formData.unit"
                    placeholder="请选择计量单位"
                  >
                    <el-option
                      v-for="item in measurement_unit"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col> -->
              <el-col :span="12">
                <el-form-item label="内部交易" prop="internalTransaction">
                  <el-radio-group v-model="formData.internalTransaction">
                    <el-radio
                      v-for="item in yes_or_no"
                      :key="item.value"
                      :label="item.value"
                      >{{ item.label }}</el-radio
                    >
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="入库单价为0时，参与成本核算"
                  prop="costAccounting"
                  label-width="120px"
                  class="leading-snug"
                >
                  <template #label="{ label }">
                    <span class="leading-snug">{{ label }}</span>
                  </template>
                  <el-radio-group v-model="formData.costAccounting">
                    <el-radio
                      v-for="item in yes_or_no"
                      :key="item.value"
                      :label="item.value"
                      >{{ item.label }}</el-radio
                    >
                  </el-radio-group>
                </el-form-item>
              </el-col>
            </el-row>
          </el-collapse-item>
          <!-- 其他信息 -->
          <el-collapse-item name="其他信息">
            <template #title>
              <TitleName
                title="其他信息"
                icon="i-carbon-bookmark-filled"
                :titleStyle="{ color: '#000' }"
              ></TitleName>
            </template>
            <el-row>
              <el-col :span="12">
                <el-form-item label="规格型号" prop="specification">
                  <el-input
                    v-model="formData.specification"
                    placeholder="请输入规格型号"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12" v-if="formData.createUnit">
                <el-form-item label="创建单位" prop="createUnit">
                  <el-tree-select
                    class="w-full"
                    v-model="formData.createUnit"
                    placeholder="请选择创建单位"
                    disabled
                    :data="companyData"
                    :checkStrictly="true"
                    :props="{
                      value: 'id',
                      label: 'label',
                      children: 'children'
                    }"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="MDM编码" prop="mdmNumber">
                  <el-input
                    v-model="formData.mdmNumber"
                    placeholder="请输入MDM编码"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="助记码" prop="mnemonic">
                  <el-input
                    v-model="formData.mnemonic"
                    placeholder="请输入助记码"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="配置" prop="configuration">
                  <el-input
                    v-model="formData.configuration"
                    placeholder="请输入配置"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="外文名称" prop="foreignName">
                  <el-input
                    v-model="formData.foreignName"
                    placeholder="请输入外文名称"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="别名" prop="alias">
                  <el-input v-model="formData.alias" placeholder="请输入别名" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="注册商标" prop="trademark">
                  <el-input
                    v-model="formData.trademark"
                    placeholder="请输入注册商标"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="税收分类" prop="taxClassification">
                  <el-input
                    v-model="formData.taxClassification"
                    placeholder="请输入税收分类"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="批准文号" prop="approvalNumber">
                  <el-input
                    v-model="formData.approvalNumber"
                    placeholder="请输入批准文号"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="税率" prop="taxRate">
                  <el-input
                    v-model="formData.taxRate"
                    placeholder="请输入税率"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="享受税收优惠政策" prop="enjoyPreferential">
                  <el-radio-group v-model="formData.enjoyPreferential">
                    <el-radio
                      v-for="item in yes_or_no"
                      :key="item.value"
                      :label="item.value"
                      >{{ item.label }}</el-radio
                    >
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="优惠政策类型" prop="preferentialType">
                  <el-input
                    v-model="formData.preferentialType"
                    placeholder="请输入优惠政策类型"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="数量精度" prop="accuracy">
                  <el-select
                    class="w-full"
                    v-model="formData.accuracy"
                    placeholder="请选择数量精度"
                  >
                    <el-option
                      v-for="item in quantity_accuracy"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="是否新物料" prop="isNew">
                  <el-radio-group v-model="formData.isNew">
                    <el-radio
                      v-for="item in yes_or_no"
                      :key="item.value"
                      :label="item.value"
                      >{{ item.label }}</el-radio
                    >
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="新物料有效年月" prop="effectiveDate">
                  <el-date-picker
                    style="width: 100%"
                    v-model="formData.effectiveDate"
                    type="date"
                    placeholder="新物料有效年月"
                    value-format="YYYY-MM-DD"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="自研物料" prop="selfProduct">
                  <el-select
                    class="w-full"
                    v-model="formData.selfProduct"
                    placeholder="请选择自研物料"
                  >
                    <el-option
                      v-for="item in self_product"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="总部管理" prop="headManage">
                  <el-radio-group v-model="formData.headManage">
                    <el-radio
                      v-for="item in yes_or_no"
                      :key="item.value"
                      :label="item.value"
                      >{{ item.label }}</el-radio
                    >
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="是否硬件" prop="isHardware">
                  <el-radio-group v-model="formData.isHardware">
                    <el-radio
                      v-for="item in yes_or_no"
                      :key="item.value"
                      :label="item.value"
                      >{{ item.label }}</el-radio
                    >
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="往来业务板块" prop="transactionPlate">
                  <el-input
                    v-model="formData.transactionPlate"
                    placeholder="请输入往来业务板块"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-collapse-item>
        </el-collapse>
      </el-form>
      <avue-form
        v-if="false"
        :option="formOption"
        v-model="formData"
        ref="form"
      >
        <template #costAccounting-label="{ column }">
          <span class="leading-snug">{{ column.label }}</span>
        </template>
      </avue-form>
    </div>

    <!-- 物料选择弹窗 -->
    <A8Product
      v-if="dialogParam.open"
      v-model="dialogParam.open"
      :dialogParam="dialogParam"
      @ok="productDialogFn"
    />
  </div>
</template>
<script setup name="ProductForm">
import { reactive, toRefs, ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import useCommonStore from '@/store/modules/common'
import { skipToError } from '@/utils/commo'
import { isMoneyNot, isNum } from '@/utils/validate'
import {
  listCategory,
  setOrdproduct,
  getOrdproduct
} from '@/api/productMange/product'
import useTagsViewStore from '@/store/modules/tagsView'
import {
  loadingSelect, //远程检索loading
  whsOptLimitEnt, // 仓库数据
  queryWhsLimitEnt, // 检索仓库数据
  order_business_type, //订单业务分类
  businessTypeOption //业务类型树
} from '@/hooks/dict/order'
const router = useRouter()
const route = useRoute()
const commonStore = useCommonStore()
const { proxy, type } = getCurrentInstance()
const sortOptions = ref([])
const companyData = ref([])
commonStore.getCompanyData().then(e => {
  companyData.value = e
})

const {
  ord_product_plate_type,
  ord_product_plate,
  yes_or_no,
  measurement_unit,
  credit_rating,
  quantity_accuracy,
  addition_method,
  device_type,
  warehouse_info,
  self_product,
  product_bussiness_plate,
  transaction_type,
  ord_sync_location
} = proxy.useDict(
  'ord_product_plate_type',
  'ord_product_plate',
  'yes_or_no',
  'measurement_unit',
  'credit_rating',
  'quantity_accuracy',
  'addition_method',
  'device_type',
  'warehouse_info',
  'self_product',
  'product_bussiness_plate',
  'transaction_type',
  'ord_sync_location'
)

const props = defineProps({
  productId: {
    type: [Number, String],
    require: true
  }
})
const data = reactive({
  activeNames: ['基本信息', '业务信息'], //, '财务信息', '业务信息', '其他信息'
  formData: {
    price: 0,
    id: '',
    laborService: '0',
    zeroExitControl: '0',
    serviceDateRequired: '0'
  },
  formRules: {
    name: [
      {
        required: true,
        message: '请输入名称',
        trigger: 'blur'
      }
    ],
    number: [
      {
        required: true,
        message: '请输入编号',
        trigger: 'blur'
      }
    ],
    categoryId: [
      {
        required: true,
        message: '请选择分类',
        trigger: 'change'
      }
    ],
    businessType: [
      {
        required: true,
        message: '请选择所属业务分类',
        trigger: 'change'
      }
    ],
    unit: [
      {
        required: true,
        message: '请选择计量单位',
        trigger: 'change'
      }
    ],
    defaultWarehouse: [
      {
        required: false,
        message: '请选择仓库',
        trigger: 'change'
      }
    ],
    price: [
      {
        required: true,
        message: '请输入销售价',
        trigger: 'change'
      },
      {
        validator: isMoneyNot
      }
    ],
    referencePrice: [
      {
        validator: isMoneyNot
      }
    ],
    safeInventory: [
      {
        validator: isNum
      }
    ],
    minInventory: [
      {
        validator: isNum
      }
    ],
    maxInventory: [
      {
        validator: isNum
      }
    ],
    purchaseReferencePrice: [
      { required: false, validator: isMoneyNot, trigger: 'blur' }
    ],
    tradingReferencePrice: [
      { required: false, validator: isMoneyNot, trigger: 'blur' }
    ],
    minSellingPrice: [
      { required: false, validator: isMoneyNot, trigger: 'blur' },
      { validator: rulMinSellingPrice, trigger: 'blur' }
    ],
    maxSellingPrice: [
      { required: false, validator: isMoneyNot, trigger: 'blur' },
      { validator: rulMaxSellingPrice, trigger: 'blur' }
    ],
    markup: [{ required: false, validator: isMoneyNot, trigger: 'blur' }]
  },

  formOption: {
    tabs: false,
    submitBtn: false,
    emptyBtn: false,
    labelWidth: '140',
    group: [
      {
        label: '基本信息',
        // labelWidth: '0',
        prop: 'group1',
        arrow: false,
        column: [
          {
            label: '名称',
            prop: 'name',
            span: 12,
            placeholder: '名称',
            type: 'input',
            rules: [
              {
                required: true,
                message: '请输入名称',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '编号',
            prop: 'number',
            span: 12,
            placeholder: '编号',
            type: 'input',
            rules: [
              {
                required: true,
                message: '请输入编号',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '分类',
            // labelWidth: '0',
            span: 12,
            prop: 'categoryId',
            placeholder: '分类',
            checkStrictly: true,
            type: 'tree',
            props: {
              value: 'id',
              label: 'name',
              children: 'children'
            },
            dicData: sortOptions,
            // focus: (column, value) => {
            //   column.column.dicData = sortOptions.value
            //   console.log('column', column)
            // },
            rules: [
              {
                required: true,
                message: '请选择分类',
                trigger: 'change'
              }
            ]
          },
          {
            label: '计量单位',
            // labelWidth: '0',
            span: 12,
            prop: 'unit',
            placeholder: '计量单位',
            type: 'select',
            dicData: measurement_unit,
            rules: [
              {
                required: true,
                message: '请选择计量单位',
                trigger: 'change'
              }
            ]
          },
          {
            label: '板块类型',
            // labelWidth: '0',
            span: 12,
            prop: 'plateType',
            placeholder: '板块类型',
            type: 'select',
            dicData: ord_product_plate_type
            // focus: () => {
            //   console.log(
            //     'ord_product_plate_type.value',
            //     ord_product_plate_type.value
            //   )
            // }
          },
          {
            label: '产业板块',
            // labelWidth: '0',
            span: 12,
            prop: 'industrialPlate',
            placeholder: '产业板块',
            type: 'select',
            dicData: ord_product_plate
          },
          {
            label: '规格型号',
            prop: 'specification',
            span: 12,
            placeholder: '规格型号',
            type: 'input',
            rules: [
              {
                required: false,
                message: '请输入规格型号',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '创建单位',
            // labelWidth: '0',
            span: 12,
            prop: 'createUnit',
            placeholder: '创建单位',
            checkStrictly: true,
            type: 'tree',
            props: {
              value: 'id',
              label: 'label',
              children: 'children'
            },
            dicData: companyData
          },
          {
            label: 'MDM编码',
            prop: 'mdmNumber',
            span: 12,
            placeholder: 'MDM编码',
            type: 'input',
            rules: [
              {
                required: false,
                message: '请输入MDM编码',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '简称',
            prop: 'abbreviation',
            span: 12,
            placeholder: '简称',
            type: 'input',
            rules: [
              {
                required: false,
                message: '请输入简称',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '助记码',
            prop: 'mnemonic',
            span: 12,
            placeholder: '助记码',
            type: 'input'
          },
          {
            label: '配置',
            prop: 'configuration',
            span: 12,
            placeholder: '配置',
            type: 'input',
            rules: [
              {
                required: false,
                message: '请输入配置',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '外文名称',
            prop: 'foreignName',
            span: 12,
            placeholder: '外文名称',
            type: 'input',
            rules: [
              {
                required: false,
                message: '请输入外文名称',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '别名',
            prop: 'alias',
            span: 12,
            placeholder: '别名',
            type: 'input',
            rules: [
              {
                required: false,
                message: '请输入别名',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '注册商标',
            prop: 'trademark',
            span: 12,
            placeholder: '注册商标',
            type: 'input',
            rules: [
              {
                required: false,
                message: '请输入注册商标',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '税收分类',
            // labelWidth: '0',
            span: 12,
            prop: 'taxClassification',
            placeholder: '税收分类',
            type: 'input'
            // dicData: credit_rating
          },
          {
            label: '批准文号',
            prop: 'approvalNumber',
            span: 12,
            placeholder: '批准文号',
            type: 'input',
            rules: [
              {
                required: false,
                message: '请输入批准文号',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '税率',
            // labelWidth: '0',
            span: 12,
            prop: 'taxRate',
            placeholder: '税收税率',
            type: 'input'
            // dicData: credit_rating.value
          },
          {
            label: '享受税收优惠政策',
            prop: 'enjoyPreferential',
            type: 'radio',
            dicData: yes_or_no
          },
          {
            label: '优惠政策类型',
            // labelWidth: '0',
            span: 12,
            prop: 'preferentialType',
            placeholder: '优惠政策类型',
            type: 'input'
            // dicData: credit_rating
          },
          {
            label: '数量精度',
            // labelWidth: '0',
            span: 12,
            prop: 'accuracy',
            placeholder: '数量精度',
            type: 'select',
            dicData: quantity_accuracy
          },
          {
            label: '劳务服务',
            prop: 'laborService',
            type: 'radio',
            dicData: yes_or_no
          },
          {
            label: '是否新物料',
            prop: 'isNew',
            type: 'radio',
            dicData: yes_or_no
          },
          {
            label: ' 新物料有效年月',
            // labelWidth: '0',
            span: 12,
            prop: 'effectiveDate',
            type: 'date',
            placeholder: '新物料有效年月',
            format: 'YYYY-MM-DD',
            valueFormat: 'YYYY-MM-DD'
          },
          {
            label: '设备类型',
            prop: 'deviceType',
            type: 'radio',
            dicData: device_type
          },
          {
            label: '自研物料',
            // labelWidth: '0',
            span: 12,
            prop: 'selfProduct',
            placeholder: '自研物料',
            type: 'select',
            dicData: self_product
          },
          {
            label: '总部管理',
            prop: 'headManage',
            type: 'radio',
            dicData: yes_or_no
          },
          {
            label: '是否硬件',
            prop: 'isHardware',
            type: 'radio',
            dicData: yes_or_no
          },
          {
            label: '物料业务板块',
            // labelWidth: '0',
            span: 12,
            prop: 'productBussinessPlate',
            placeholder: '物料业务板块',
            type: 'select',
            dicData: product_bussiness_plate
          },
          {
            label: '往来业务板块',
            prop: 'transactionPlate',
            span: 12,
            placeholder: '往来业务板块',
            type: 'input',
            rules: [
              {
                required: false,
                message: '请输入往来业务板块',
                trigger: 'blur'
              }
            ]
          }

          /* {
            label: '上传合同',
            prop: 'imgUrl',
            type: 'upload',
            span: 12,
            fileText: '上传合同',
            propsHttp: {
              url: 'url',
              name: 'name',
              res: 'data'
            },
            action: action
          }, */
        ]
      },
      {
        label: '财务信息',
        // labelWidth: '0',
        prop: 'cwxx',
        arrow: false,
        column: [
          {
            label: '参考成本',
            prop: 'referencePrice',
            span: 12,
            placeholder: '参考成本',
            type: 'input',
            rules: [
              {
                required: false,
                message: '请输入参考成本',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '收入分摊',
            prop: 'incomeShare',
            type: 'radio',
            dicData: yes_or_no
          },
          {
            label: '开票名称',
            prop: 'invoiceName',
            span: 12,
            placeholder: '开票名称',
            type: 'input',
            rules: [
              {
                required: false,
                message: '请输入开票名称',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '批次核算',
            prop: 'batchAccounting',
            type: 'radio',
            dicData: yes_or_no
          },
          {
            label: '开票规格型号',
            prop: 'invoiceType',
            span: 12,
            placeholder: '开票规格型号',
            type: 'input',
            rules: [
              {
                required: false,
                message: '请输入开票规格型号',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '项目核算',
            prop: 'projectAccounting',
            type: 'radio',
            dicData: yes_or_no
          },
          {
            label: '计量单位',
            // labelWidth: '0',
            span: 12,
            prop: 'unit',
            placeholder: '计量单位',
            type: 'select',
            dicData: measurement_unit
          },
          {
            label: '内部交易',
            prop: 'internalTransaction',
            type: 'radio',
            dicData: yes_or_no
          },
          {
            label: '入库单价为0时，参与成本核算',
            prop: 'costAccounting',
            type: 'radio',
            labelWidth: '120',
            dicData: yes_or_no
          }
        ]
      },
      {
        label: '业务信息',
        // labelWidth: '0',
        prop: 'ywxx',
        arrow: false,
        column: [
          {
            label: '销售价',
            // labelWidth: '0',
            prop: 'price',
            span: 12,
            placeholder: '销售价',
            append: '元',
            type: 'input',
            rules: [
              {
                required: true,
                message: '请输入销售价',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '默认仓库',
            // labelWidth: '0',
            span: 12,
            prop: 'defaultWarehouse',
            placeholder: '默认仓库',
            type: 'select',
            dicData: warehouse_info
          },
          {
            label: '采购参考价格',
            prop: 'purchaseReferencePrice',
            span: 12,
            placeholder: '采购参考价格',
            type: 'input',
            rules: [
              {
                required: false,
                message: '请输入采购参考价格',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '默认货位',
            prop: 'defaultCargo',
            span: 12,
            placeholder: '默认货位',
            type: 'input',
            rules: [
              {
                required: false,
                message: '请输入默认货位',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '内部交易参考价格',
            prop: 'tradingReferencePrice',
            span: 12,
            placeholder: '内部交易参考价格',
            type: 'input',
            rules: [
              {
                required: false,
                message: '请输入内部交易参考价格',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '最低销售价格',
            // labelWidth: '0',
            prop: 'minSellingPrice',
            span: 12,
            placeholder: '最低销售价格',
            append: '元',
            type: 'input',
            rules: [
              {
                required: false,
                message: '请输入最低销售价格',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '超订单入库比例',
            // labelWidth: '0',
            prop: 'excessOrderInventoryRatio',
            span: 12,
            placeholder: '超订单入库比例',
            // append: '元',
            type: 'input',
            rules: [
              {
                required: false,
                message: '请输入超订单入库比例',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '最高销售价格',
            // labelWidth: '0',
            prop: 'maxSellingPrice',
            span: 12,
            placeholder: '最高销售价格',
            append: '元',
            type: 'input',
            rules: [
              {
                required: false,
                message: '请输入最高销售价格',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '成本加成方式',
            // labelWidth: '0',
            span: 12,
            prop: 'additionType',
            placeholder: '成本加成方式',
            type: 'select',
            dicData: addition_method
          },
          {
            label: '成本加成比率',
            // labelWidth: '0',
            prop: 'additionRatio',
            span: 12,
            placeholder: '成本加成比率',
            // append: '元',
            type: 'input',
            rules: [
              {
                required: false,
                message: '请输入成本加成比率',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '往来业务类型',
            // labelWidth: '0',
            span: 12,
            prop: 'transactionType',
            placeholder: '往来业务类型',
            type: 'select',
            dicData: transaction_type
          },
          {
            label: '成本加价',
            prop: 'markup',
            span: 12,
            placeholder: '成本加价',
            type: 'input',
            rules: [
              {
                required: false,
                message: '请输入成本加价',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '安全库存',
            prop: 'safeInventory',
            span: 12,
            placeholder: '安全库存',
            type: 'input',
            rules: [
              {
                required: false,
                message: '请输入安全库存',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '最低库存',
            prop: 'minInventory',
            span: 12,
            placeholder: '最低库存',
            type: 'input',
            rules: [
              {
                required: false,
                message: '请输入最低库存',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '最高库存',
            prop: 'maxInventory',
            span: 12,
            placeholder: '最高库存',
            type: 'input',
            rules: [
              {
                required: false,
                message: '请输入最高库存',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '组装件',
            prop: 'assembly',
            type: 'radio',
            dicData: yes_or_no
          }

          /* {
            label: '上传合同',
            prop: 'imgUrl',
            type: 'upload',
            span: 12,
            fileText: '上传合同',
            propsHttp: {
              url: 'url',
              name: 'name',
              res: 'data'
            },
            action: action
          }, */
        ]
      }
    ]
  }
})
const { formData, formRules, formOption, activeNames } = toRefs(data)
// watch(
//   () => [formOption, route.query.productId], //[passData.data, data.crudData],
//   (newVal, oldValue) => {
//     console.log('newVal[1]', newVal[1])
//   },
//   { deep: true }
// )
watch(
  [
    () => route.query.productId,
    () => route.query.categoryId,
    () => props.productId
  ],
  () => {
    if (route.query.productId && route.name == type.name) {
      console.log('route.query', route)
      getFormData(route.query.productId)
    }
    // if (route.query.categoryId && route.name == type.name) {
    //   console.log(
    //     'route.query.categoryId',
    //     route.query.categoryId,
    //     typeof route.query.categoryId
    //   )
    //   formData.value.categoryId = Number(route.query.categoryId)
    // }
    if (props.productId) {
      getFormData(props.productId)
    }
  },
  {
    immediate: true
  }
)

// 校验最低销售价格
function rulMinSellingPrice(rule, value, callback) {
  if (
    value &&
    formData.value.maxSellingPrice &&
    value > formData.value.maxSellingPrice
  ) {
    callback(new Error('最低销售价格不能高于最高销售价'))
  } else {
    callback()
  }
}
// 校验最高销售价
function rulMaxSellingPrice(rule, value, callback) {
  if (
    value &&
    formData.value.minSellingPrice &&
    value < formData.value.minSellingPrice
  ) {
    callback(new Error('最高销售价不能低于最低销售价格'))
  } else {
    callback()
  }
}
/** 获取分类下拉树结构 */
function getTreeselect() {
  sortOptions.value = []
  // let tree = {
  //   id: 0,
  //   name: '根目录',
  //   children: []
  // }
  listCategory().then(res => {
    sortOptions.value = res.data
    // formOption.value.group[1].column[2].dicData = sortOptions.value
  })
}
/** 获取物料详情
 * @param {number}  id 表单id
 */
function getFormData(id) {
  getOrdproduct(id).then(res => {
    formData.value = res.data
    if (formData.value.defaultWarehouse) {
      whsOptLimitEnt.value = [
        {
          id: formData.value.defaultWarehouse,
          name: formData.value.warehouseName
        }
      ]
    }
  })
}

const dialogParam = ref({
  open: false
})
// 添加产品
function addProduct() {
  dialogParam.value.open = true
  dialogParam.value.productQuery = {}
}
// 确定添加产品
function productDialogFn(val) {
  // console.log('val===>', val)
  let { name, number, taxRate, specification, taxClassification, unit } = val[0]
  // console.log('val===>', name, number, taxRate, specification)
  formData.value = {
    ...formData.value,
    name,
    number,
    taxRate,
    specification,
    taxClassification,
    unit
  }
}

// 初始化
function init() {
  if (route.query.categoryId) {
    formData.value.categoryId = Number(route.query.categoryId)
  }
  console.log('物料表单==》', route.query.productId)
  getTreeselect()
}
function handleSubmit(e) {
  console.log('proxy.$refs.form=>', proxy.$refs.form)
  proxy.$refs.form.validate((valid, ValidateFieldsError) => {
    if (valid) {
      setOrdproduct(formData.value).then(res => {
        // if (!formData.value.id) {
        //   useTagsViewStore().delView(router.currentRoute.value)
        //   router.go(-1)
        // }
        // proxy.$modal.msgSuccess(formData.value.id ? '修改成功' : '新增成功')
        // 保存接口才有
        proxy.useReturn(
          '',
          () => {
            //返回上一步
            handleReturn()
          },
          async () => {
            //继续
          }
        )
        formData.value = res.data
      })
    } else {
      skipToError()
    }
  })
}
function handleReset() {
  console.log('清除')
  formData.value = {
    price: 0
  }

  proxy.resetForm('sortRef')
}
function handleReturn() {
  useTagsViewStore().delView(router.currentRoute.value)
  router.go(-1)
  // router.push({ path: url, query: data })
  //   proxy.$refs.form.clearValidate()
}

init()
</script>
<style scoped>
.form-title {
  color: #111111;
}
.product-form:deep(.form-title:before) {
  content: '▋';
  display: inline-block;
  color: #409eff;
}
</style>
